using System;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Bll
{
    public class AdController : BaseController
    {
        private static IAdContent ConvertCmsObject2Ad(ICmsObject obj)
        {
            IAdContent ad = new AdContent();
            ad.FromCmsObject(obj);
            return ad;
        }

        private static IList<IAdContent> ConvertListObject2ListAd(IList<ICmsObject> list)
        {
            IList<IAdContent> ads = new List<IAdContent>();
            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    ads.Add(ConvertCmsObject2Ad(obj));
                }

            return ads;
        }

        private static AdContentList ConvertListObject2ListAdByPageID(IList<ICmsObject> list,long pageID)
        {
            AdContentList ads = new AdContentList();
            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    IAdContent ad = new AdContent();
                    ad =ConvertCmsObject2Ad(obj);
                    ad = GetAd(ad.ID);
                    foreach(IPage p in ad.PageList)
                        if (p.ID ==pageID)
                        ads.Add(ad);
                }

            return ads;
        }

        

        public static IList<IAdContent> GetAdList(long classID)
        {
            return ConvertListObject2ListAd(_Ds.GetObjectListByClassID(classID));
        }


        public static AdContentList GetAdListByPageID(long pageID)
        {

            return ConvertListObject2ListAdByPageID(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.AdContent), pageID);
        }

        public static IAdContent GetAd(long id)
        {
            IAdContent ad = ConvertCmsObject2Ad(_Ds.GetObject(id));
            if (ad != null)
                // ad.PageList = PageController.GetObjectListRelateToObjectID(id);
                ad.PageList = PageController.GetPageListRelateToObjectID(id);
            return ad;
        }

        public static long InsertAd(IAdContent ad)
        {
            long id= _Ds.InsertObject((ICmsObject)ad);
            InsertRelationPageList(ad.PageList, id);
            return id;
        }

        private static void InsertRelationPageList(PageList list, long adID)
        {
            foreach (IPage page in list)
            {
                try
                {
                    _Ds.InsertObjectRelation(adID, page.ID, (short)CmsObjectType.AdContent);
                }
                catch { }
            }
        }


        public static void UpdateAd(IAdContent ad)
        {
            ICmsObject obj = _Ds.GetObject(ad.ID);
            PageList pageList = PageController.ConvertListObject2PageList(obj.SubObjectList);

            _Ds.UpdateObject((ICmsObject)ad);

            IList<long> listOld = new List<long>();
            foreach (IPage page in pageList)
            {
                listOld.Add(page.ID);
            }
            IList<long> listNew = new List<long>();
            foreach (IPage page in ad.PageList)
            {
                listNew.Add(page.ID);
            }
            _Ds.UpdateObjectRelation(listOld, listNew, ad.ID, (short)CmsObjectType.AdContent);

        }

        private static void DeleteRelationPageList(PageList list, long adID)
        {
            foreach (IPage page in list)
            {
                try
                {
                    _Ds.DeleteObjectOneRelation(adID, page.ID);
                }
                catch { }
            }
        }

        public static void DeleteAd(long id)
        {
            IAdContent ad = GetAd(id);
            _Ds.DeleteObject(id);
            DeleteRelationPageList(ad.PageList, id);
        }
    }
}
